package SSM.Chat.Controller;


import SSM.Chat.Domain.*;
import SSM.Chat.Service.BookService;
import SSM.Chat.Utils.Code;
import SSM.Chat.Utils.Mailbox;
import SSM.Chat.Utils.Request;
import SSM.Chat.Utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@RestController
// 将web变成bean
// ，并将最后的return
// 的数据作为整体返回给前端
//@RequestMapping
// ("/books") //
// 用于修饰类，则是该类所有方法的总路径，修饰方法则是方法的路径


public class BookWeb {
    @Autowired
    //自动装配
    private BookService bookService;

    //

    //   判断当前是否断网/token权限过期
    @PostMapping("/Flag")
    public Result Flag(HttpServletRequest request, HttpServletResponse response) {
        Request request1 = new Request();
        String axios = request1.Axios(request);
        if (axios == null) {
            response.setStatus(401);
            return new Result(Code.TokenLimits.getValue(), "您没有登录权限");
        }
        Token token = new Token();
        String token1 = token.getToken(axios);
        if (token1.equals("401")) {
            response.setStatus(401);
            return new Result(Code.TokenLimits.getValue(), "登录超时");
        } else if (token1 == null) {
            response.setStatus(401);
            return new Result(Code.TokenLimits.getValue(), "您没有权限");
        }

        return new Result(Code.SUCCESS.getValue(), "验证成功");
    }

    // 根据token获取登录人员所有配置信息验证信息
    @PostMapping("/SettingSelectAll")
    public Result SettingSelectAll(HttpServletRequest request, HttpServletResponse response) {
        Request request1 = new Request();
        String axios = request1.Axios(request);
        Token token = new Token();
        String token1 = token.getToken(axios);
        if (token1 == null) {
            response.setStatus(401);
            return new Result(Code.TokenLimits.getValue(), "您没有权限");
        } else if (token1.equals("401")) {
            return new Result(Code.TokenLimits.getValue(), "登录超时");
        }
        User user = bookService.UserSelectMailbox(token1);
        if (user == null) {
            response.setStatus(401);
            return new Result(Code.TokenLimits.getValue(), "您没有权限");
        }
        Settings settings = bookService.SettingSelectMailbox(token1);
        if (settings == null) {
            response.setStatus(401);
            return new Result(Code.TokenLimits.getValue(), "您没有权限");
        }
        return new Result(Code.SUCCESS.getValue(), "查询成功", settings);
    }

    // 获取验证码
    @PostMapping("/GetCode")
    public Result GetCode(@RequestBody AuthCode authCode) throws MessagingException {
        // 注册时判断该邮箱是否存在
        // 当注册时判断该邮箱是否存在
        if (authCode.getAuthCodeType().equals("Register")) {
            User user = bookService.SelectUserMailbox(authCode.getAuthCodeMailbox());
            if (user != null) {
                return new Result(Code.ERROR.getValue(), "该邮箱已经注册过，无法重复注册");
            }
        }
        // 当忘记密码时判断该邮箱是否存在
        if (authCode.getAuthCodeType().equals("Forgot")) {
            User user = bookService.SelectUserMailbox(authCode.getAuthCodeMailbox());
            if (user == null) {
                return new Result(Code.ERROR.getValue(),
                        "该邮箱不存在，无法修改密码");
            }
        }

        String[] arr = {"qq.com", "163.com"};
        String[] filter = new String[1];
        Pattern compile = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
        Matcher matcher = compile.matcher(authCode.getAuthCodeMailbox());
        boolean matches = matcher.matches();
        if (matches) {
            String str2 = "";
            for (int i = 0; i < 10; i++) {
                str2 += Integer.valueOf((int) Math.floor(Math.random() * 8 + 1));
            }
            Mailbox mailbox = new Mailbox();
            mailbox.SendMailbox(authCode.getAuthCodeMailbox(), authCode.getAuthCodeType(), str2);
            authCode.setAuthCodeUse(1);
            authCode.setAuthCodeMailbox(authCode.getAuthCodeMailbox().toLowerCase());
            authCode.setAuthCodeText(str2);
            authCode.setAuthCodeTime(new Date());
            bookService.AutoCodeAdd(authCode);

        } else {
            return new Result(Code.ERROR.getValue(), "您输入的邮箱号不正确");
        }
        return new Result(Code.SUCCESS.getValue(), "发送验证码成功");
    }

    //   注册
    @PostMapping("/Register")
    public Result Login(@RequestBody Register register) throws MessagingException {

        AuthCode authCode = new AuthCode();
        authCode.setAuthCodeMailbox(register.getUserMailbox());
        authCode.setAuthCodeType(register.getAuthCodeType());
        authCode.setAuthCodeText(register.getAuthCodeText());
        AuthCode authCode1 = bookService.SelectTypeAutoCode(authCode);
        if (authCode1 == null) {
            return new Result(Code.ERROR.getValue(), "您输入的验证码不存在");
        }
        if (authCode1.getAuthCodeUse() == 2) {
            return new Result(Code.ERROR.getValue(), "您的验证码已经被使用");
        }
        authCode1.setAuthCodeUse(2);

        char cha[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Integer[] num = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Object[] arr = new Object[10];
        // 生成数字个数
        Random random = new Random();
        //设置随机生成的数字数量
        for (int i = 0; i < 5; i++) {
            arr[i] = num[random.nextInt(10)];
        }
        // 设置随机生成的大写字母数量
        for (int i = 5; i < 8; i++) {
            arr[i] = String.valueOf(cha[random.nextInt(26)]);
        }
        // 设置随机生成的小写字母数量
        for (int i = 8; i < 10; i++) {
            arr[i] = String.valueOf(cha[random.nextInt(26)]).toLowerCase();
        }
        String str = "";
        for (int i = 0; i < arr.length; i++) {
            str += arr[random.nextInt(arr.length)];
        }
        // 设置token
//        Token token = new Token();
//        String tokenText = token.setToken(register.getUserMailbox());
        // 添加数据库个人信息
        User user = new User();
        user.setUserMailbox(register.getUserMailbox().toLowerCase());
        user.setUserNickname(register.getUserNickname());
        user.setUserTime(new Date());
        user.setUserPassword(DigestUtils.md5DigestAsHex(str.getBytes()));
        // 添加数据库个人配置
        Settings settings = new Settings();
        settings.setSettingMailbox(register.getUserMailbox().toLowerCase());
        settings.setSettingSidebar("bottom");
        settings.setSettingTheme("light");
        bookService.UserAdd(user);
        bookService.SettingAdd(settings);
        Boolean aBoolean = bookService.AutoCodeUpdateUse(authCode1);
        if (aBoolean) {
            // 发送邮件
            Mailbox mailbox = new Mailbox();
            mailbox.SendMailbox(register.getUserMailbox(), "Login", str);
        }
        return new Result(Code.SUCCESS.getValue(), "注册成功,初始密码已经发送至您邮箱中");
    }


    // 获取验证码
    @PostMapping("/UserLogin")
    public Result UserLogin(@RequestBody User user) {
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        User user1 = bookService.UserLogin(user);
        if (user1 == null) {
            return new Result(Code.ERROR.getValue(), "账号或者密码错误");
        }

        Token token = new Token();
        String token1 = token.setToken(user1.getUserMailbox());
        return new Result(Code.SUCCESS.getValue(), "登录成功", token1);
    }

    // 忘记密码
    @PostMapping("/Forgot")
    @Transactional(value = "To")
    public Result Forgot(@RequestBody Register register) {
        User user = bookService.SelectUserMailbox(register.getUserMailbox().toLowerCase());
        if (user == null) {
            return new Result(Code.ERROR.getValue(), "该邮箱不存在");
        }
        AuthCode authCode = new AuthCode();
        authCode.setAuthCodeMailbox(register.getUserMailbox());
        authCode.setAuthCodeType(register.getAuthCodeType());
        authCode.setAuthCodeText(register.getAuthCodeText());
        AuthCode authCode1 = bookService.SelectTypeAutoCode(authCode);
        if (authCode1 == null) {
            return new Result(Code.ERROR.getValue(), "验证码不存在");
        }
        if (authCode1.getAuthCodeUse() != 1) {
            return new Result(Code.ERROR.getValue(), "验证码已经被使用");
        }
        authCode.setAuthCodeUse(2);
        user.setUserPassword(DigestUtils.md5DigestAsHex((register.getUserPassword()).getBytes()));
        bookService.AutoCodeUpdateUse(authCode);
        bookService.UserUpdatePassword(user);
        return new Result(Code.SUCCESS.getValue(), "修改成功");
    }


    // 查询所有好友及其分类
    @PostMapping("/BuddyAll")
    public Result BuddyAll(HttpServletRequest request, HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 查询好友表
        List<Buddy> buddies = bookService.BuddySelectOneself(leave);
        List<Group> groups = bookService.GroupSelectUserId(leave);
        if (buddies.size() <= 0) {
            return new Result(Code.SUCCESS.getValue(), "您还没有任何好友", groups);
        }
        // 查询分组表
        // 将好友列表中的内容匹配到分组表
        groups.forEach(el -> {
            // 好友表中相同的分类过滤出来
            List<Buddy> collect =
                    buddies.stream().filter(el1 -> el1.getBuddyGroup().equals(el.getGroupId().toString())).collect(Collectors.toList());
            if (collect.size() > 0) {
                // 获取所有好友列表id
                String[] strings = new String[collect.size()];
                for (int i = 0; i < collect.size(); i++) {
                    strings[i] = (collect.get(i).getBuddyOthersId());
                }
                // 批量查询好友信息
                List<User> users = bookService.UserBatchSelect(strings);
                // 判断当前分组是否是黑名单分组
                if (el.getGroupName().equals("黑名单")) {
                    for (int i = 0; i < users.size(); i++) {
                        users.get(i).setUserMailboxType("4");
                    }
                }
                // 将查询到的好友信息添加到数据中
                el.setChildren(users);
            }
        });
        return new Result(Code.SUCCESS.getValue(), "查询成功", groups);
    }

    // 好友推荐
    @PostMapping("/BuddyRecommend")
    public Result BuddyRecommend(HttpServletRequest request, HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 查询好友表
        List<Buddy> buddies = bookService.BuddySelectOneself(leave);

        if (buddies.size() <= 0) {
            // 没有好友的时候排除自己
            String[] strings = new String[1];
            strings[0] = leave;
            List<User> users = bookService.UserNotBatchSelect(strings, leave);
            List<Await> awaits = bookService.AwaitSelectPrimary(leave);
            if (awaits.size() > 0) {
                String str = "";
                for (int i = 0; i < awaits.size(); i++) {
                    str += awaits.get(i).getAwaitSecondary();
                }
                for (int i = 0; i < users.size(); i++) {
                    if (str.indexOf(users.get(i).getUserMailbox()) != -1) {
                        users.get(i).setUserMailboxType("3");
                    } else {
                        users.get(i).setUserMailboxType("2");
                    }
                }
            } else {
                for (int i = 0; i < users.size(); i++) {
                    users.get(i).setUserMailboxType("2");
                }
            }
            Buddy buddy = new Buddy();
            for (int i = 0; i < users.size(); i++) {
                buddy.setBuddyOneselfId(leave);
                buddy.setBuddyOthersId(users.get(i).getUserMailbox());
                Buddy buddy1 = bookService.BuddySelectInformation(buddy);
                buddy.setBuddyOneselfId(users.get(i).getUserMailbox());
                buddy.setBuddyOthersId(leave);
                Buddy buddy2 =
                        bookService.BuddySelectInformation(buddy);
                // 当好友表不存在自己的时候，而存在对方的时候，我被对方拉黑
                if (buddy1 == null && buddy2 != null) {
                    users.get(i).setUserMailboxType("4");
                }
                // 当好友表存在自己但不存在对方的时候 对方被我拉黑
                if (buddy1 != null && buddy2 == null) {
                    users.get(i).setUserMailboxType("5");
                }
            }
            if (users.size() < 10) {
                return new Result(Code.SUCCESS.getValue(), "查询成功", users);
            } else {
                List<User> users1 = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    users1.add(users.get(i));
                }
                return new Result(Code.SUCCESS.getValue(), "查询成功", users1);
            }
        } else {
            String[] strings = new String[buddies.size()];
            // 有好友的时候获取所有的好友邮箱
            for (int i = 0; i < buddies.size(); i++) {
                strings[i] = buddies.get(i).getBuddyOthersId();
            }
            List<User> users = bookService.UserNotBatchSelect(strings, leave);
            List<Await> awaits = bookService.AwaitSelectPrimary(leave);
            if (awaits.size() > 0) {
                String str = "";
                for (int i = 0; i < awaits.size(); i++) {
                    str += awaits.get(i).getAwaitSecondary();
                }
                for (int i = 0; i < users.size(); i++) {
                    if (str.indexOf(users.get(i).getUserMailbox()) != -1) {
                        users.get(i).setUserMailboxType("3");
                    } else {
                        users.get(i).setUserMailboxType("2");
                    }
                }
            } else {
                for (int i = 0; i < users.size(); i++) {
                    users.get(i).setUserMailboxType("2");
                }
            }
            Buddy buddy = new Buddy();
            for (int i = 0; i < users.size(); i++) {
                buddy.setBuddyOneselfId(leave);
                buddy.setBuddyOthersId(users.get(i).getUserMailbox());
                Buddy buddy1 = bookService.BuddySelectInformation(buddy);
                buddy.setBuddyOneselfId(users.get(i).getUserMailbox());
                buddy.setBuddyOthersId(leave);
                Buddy buddy2 =
                        bookService.BuddySelectInformation(buddy);
                // 当好友表不存在自己的时候，而存在对方的时候，我被对方拉黑
                if (buddy1 == null && buddy2 != null) {
                    users.get(i).setUserMailboxType("4");
                }
                // 当好友表存在自己但不存在对方的时候 对方被我拉黑
                if (buddy1 != null && buddy2 == null) {
                    users.get(i).setUserMailboxType("5");
                }
            }
            if (users.size() < 10) {
                return new Result(Code.SUCCESS.getValue(), "查询成功", users);
            } else {
                List<User> users1 = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    users1.add(users.get(i));
                }


                return new Result(Code.SUCCESS.getValue(), "查询成功", users1);
            }

        }
    }


    // 模糊查询好友
    @PostMapping("/UserBlurSelect")
    public Result UserBlurSelect(HttpServletRequest request,
                                 HttpServletResponse response,
                                 @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 设置邮箱
        user.setUserMailbox(leave);
        // 查询需要查询的好友
        List<User> buddies = bookService.UserBlurSelect(user);
        // 查询好友列表
        List<Buddy> buddies1 = bookService.BuddySelectOneself(leave);
        if (buddies1.size() > 0) {
            String str = "";
            for (int i = 0; i < buddies1.size(); i++) {
                str += buddies1.get(i).getBuddyOthersId();
            }
            // 当查询的账户为好友的时候将userMailboxType设置成1否则为2
            for (int i = 0; i < buddies.size(); i++) {
                if (str.indexOf(buddies.get(i).getUserMailbox()) != -1) {
                    buddies.get(i).setUserMailboxType("1");
                }
            }
        }

        List<Await> awaits = bookService.AwaitSelectPrimary(leave);
        if (awaits.size() > 0) {
            String str1 = "";
            for (int i = 0; i < awaits.size(); i++) {
                str1 += awaits.get(i).getAwaitSecondary();
            }
            for (int i = 0; i < buddies.size(); i++) {
                if (str1.indexOf(buddies.get(i).getUserMailbox()) != -1) {
                    buddies.get(i).setUserMailboxType("3");
                }
            }
        }


        Buddy buddy = new Buddy();
        for (int i = 0; i < buddies.size(); i++) {
            buddy.setBuddyOneselfId(leave);
            buddy.setBuddyOthersId(buddies.get(i).getUserMailbox());
            Buddy buddy1 = bookService.BuddySelectInformation(buddy);
            buddy.setBuddyOneselfId(buddies.get(i).getUserMailbox());
            buddy.setBuddyOthersId(leave);
            Buddy buddy2 =
                    bookService.BuddySelectInformation(buddy);
            // 当好友表不存在自己的时候，而存在对方的时候，我被对方拉黑
            if (buddy1 == null && buddy2 != null) {
                buddies.get(i).setUserMailboxType("4");
            }
            // 当好友表存在自己但不存在对方的时候 对方被我拉黑
            if (buddy1 != null && buddy2 == null) {
                buddies.get(i).setUserMailboxType("5");
            }
        }
        for (int i = 0; i < buddies.size(); i++) {
            if (buddies.get(i).getUserMailboxType() == null) {
                buddies.get(i).setUserMailboxType("2");
            }

        }

        return new Result(Code.SUCCESS.getValue(), "查询成功", buddies);


    }


    // 根据邮箱查询所有分组
    @PostMapping("/GroupSelectMailbox")
    public Result GroupSelectMailbox(HttpServletRequest request,
                                     HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        // 查询分组
        List<Group> groups = bookService.GroupSelectUserId(leave);
//        查询是否存在黑名单分组
        Group group = new Group();
        group.setGroupUserId(leave);
        group.setGroupName("黑名单");
        Group group1 = bookService.GroupSelectName(group);
        // 当黑名单分组存在，过滤掉，否则不操作
        if (group1 != null) {
            groups = groups.stream().filter(el -> el.getGroupId() != group1.getGroupId()).collect(Collectors.toList());
        }

        return new Result(Code.SUCCESS.getValue(), "查询成功", groups);


    }

    // 好友申请
    @PostMapping("/AddBuddy")
    @Transactional(value = "To")
    public Result AddBuddy(HttpServletRequest request,
                           HttpServletResponse response,
                           @RequestBody Buddy buddy) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        Group group1 = new Group();
        Group group3 = new Group();
        // 判断分类是否存在
        // 当不存在分组id的时候
        String str = "";

        if (buddy.getBuddyGroup().equals(str)) {
            // 判断新分类名称是否存在
            if (!buddy.getBuddyOneselfId().equals(str) && !buddy.getBuddyOneselfId().equals("黑名单")) {
                // 判断分组是否到达上线，默认一个人最多同时存在十条分组加一个黑名单
                List<Group> groups = bookService.GroupSelectUserId(leave);
                if (groups.size() > 0) {
                    Integer index = 0;
                    Integer Size = 0;
                    // 判断是否存在黑名单
                    for (int i = 0; i < groups.size(); i++) {
                        if (groups.get(i).getGroupName().equals(
                                "黑名单")) {
                            index = 1;
                        }
                    }
                    if (index == 1) {
                        Size = 11;
                    } else {
                        Size = 10;
                    }
                    if (groups.size() >= Size) {
                        return new Result(Code.ERROR.getValue(),
                                "您分组数量已经到达上线");
                    }
                }
                // 当分类没有达到上线，判断新增分类是否存在
                Group group = new Group();
                group.setGroupName(buddy.getBuddyOneselfId());
                group.setGroupUserId(leave);
                group1 = bookService.GroupSelectName(group);
                if (group1 == null) {
                    // 当分组不存在添加分组
                    Group group2 = new Group();
                    group2.setGroupUserId(leave);
                    group2.setGroupTime(new Date());
                    group2.setGroupName(buddy.getBuddyOneselfId());
                    bookService.GroupInformation(group2);
                    // 添加完成之后查询刚刚添加的分组信息
                    group3 = bookService.GroupSelectName(group);

                } else {
                    return new Result(Code.ERROR.getValue(),
                            "您输入的分组名称已经存在，请重新输入",
                            groups);
                }
            } else {
                return new Result(Code.ERROR.getValue(),
                        "您输入的分类有误，或者为黑名单");
            }
        } else {

            Group group = new Group();
            group.setGroupId(Integer.valueOf(buddy.getBuddyGroup()));

            group3 = bookService.GroupSelectId(group);
        }

        // 分组无误后添加好友申请

        Await await = new Await();
        await.setAwaitPrimary(leave);
        await.setAwaitSecondary(buddy.getBuddyOthersId());
        await.setAwaitState(1);
        await.setAwaitTime(new Date());
        await.setAwaitType(1);
        await.setAwaitGroup(group3.getGroupName());
        // 添加申请人记录
        bookService.AwaitInformation(await);
        //添加被申请人记录
        await.setAwaitPrimary(buddy.getBuddyOthersId());
        await.setAwaitSecondary(leave);
        await.setAwaitType(2);
        await.setAwaitGroup("");
        bookService.AwaitInformation(await);
//        Buddy buddy1 = new Buddy();
//        buddy1.setBuddyGroup(group3.getGroupId().toString());
//        buddy1.setBuddyOneselfId(leave);
//        buddy1.setBuddyTime(new Date());
//        buddy1.setBuddyOthersId(buddy.getBuddyOthersId());
//        bookService.BuddyInformation(buddy1);

        return new Result(Code.SUCCESS.getValue(), "申请成功");


    }


    // 查询当前申请人数
    @PostMapping("/ApplyNumber")
    public Result ApplyNumber(HttpServletRequest request,
                              HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        List<Await> awaits = bookService.AwaitSelectPrimaryType(leave, "1");
        List<Await> awaits1 =
                bookService.AwaitSelectPrimaryType(leave, "2");
        HashMap<Object, Object> map = new HashMap<>();
        map.put("Primary", awaits.size());
        map.put("Secondary", awaits1.size());
        map.put("total", awaits1.size() + awaits.size());
        return new Result(Code.SUCCESS.getValue(), "查询成功", map);
    }


    // 查询申请好友信息
    @PostMapping("/ApplyBuddyInformation")
    public Result ApplyBuddyInformation(HttpServletRequest request,
                                        HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        List<Await> awaits = bookService.AwaitSelectPrimary(leave);
        if (awaits.size() <= 0) {
            return new Result(Code.SUCCESS.getValue(), "查询成功", awaits);
        }
//        bookService.UserBatchSelect();
        List users = new ArrayList<>();
        for (int i = 0; i < awaits.size(); i++) {
            User user = bookService.UserSelectMailbox(awaits.get(i).getAwaitSecondary());
            user.setUserMailboxType(awaits.get(i).getAwaitType().toString());
            user.setUserId(awaits.get(i).getAwaitState());
            users.add(user);
        }
        return new Result(Code.SUCCESS.getValue(), "查询成功", users);
    }


    // 拒绝好友申请
    @PostMapping("/AwaitUpdateState")
    public Result AwaitUpdateState(HttpServletRequest request,
                                   HttpServletResponse response, @RequestBody Await await) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        await.setAwaitPrimary(leave);
        await.setAwaitState(3);
        bookService.AwaitUpdateState(await);
        Await await1 = new Await();
        await1.setAwaitPrimary(await.getAwaitSecondary());
        await1.setAwaitSecondary(leave);
        await1.setAwaitState(3);
        bookService.AwaitUpdateState(await1);
        return new Result(Code.SUCCESS.getValue(), "修改成功");
    }


    // 同意好友申请
    @PostMapping("/AwaitConfirm")
    @Transactional(value = "To")
    public Result AwaitConfirm(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 获取最后的分组信息
        Group groupList = new Group();
        // 获取当前主要申请信息
        Await await = new Await();
        await.setAwaitSecondary(leave);
        await.setAwaitPrimary(user.getUserMailbox());
        Await await1 = bookService.AwaitSelectMailbox(await);
        // 根据分类内容和邮箱判断分组是否存在
        Group group = new Group();
        group.setGroupName(await1.getAwaitGroup());
        group.setGroupUserId(user.getUserMailbox());
        Group group1 = bookService.GroupSelectName(group);
        //判断分组是否存在
        if (group1 == null) {
            // 不存在判断邮箱是否到达上线
            List<Group> groups = bookService.GroupSelectUserId(user.getUserMailbox());
            group.setGroupName("黑名单"); // 判断黑名单是否存在
            Group group2 = bookService.GroupSelectName(group);
            if (group2 != null) {
                // 当黑名单存在判断分类是否高于上线
                if (groups.size() < 11) {
                    // 如果小于11 分组未到达上线，创建分组
                    Group group3 = new Group();
                    group3.setGroupName(await1.getAwaitGroup());
                    group3.setGroupTime(new Date());
                    group3.setGroupUserId(user.getUserMailbox());
                    bookService.GroupInformation(group3);
                    // 获取分组之后将分组信息提取出来
                    groupList = bookService.GroupSelectName(group3);
                } else {
                    // 要是分组存在高于上线修改请求状态，将请求状态修改为4
                    await1.setAwaitState(4);
                    bookService.AwaitUpdateState(await1);
                    return new Result(Code.BusinessException.getValue(), "添加失败");
                }
            } else {
                // 当黑名单不存在
                if (groups.size() < 10) {
                    // 如果小于11 分组未到达上线，创建分组
                    Group group3 = new Group();
                    group3.setGroupName(await1.getAwaitGroup());
                    group3.setGroupTime(new Date());
                    group3.setGroupUserId(user.getUserMailbox());
                    bookService.GroupInformation(group3);
                    // 获取分组之后将分组信息提取出来
                    groupList = bookService.GroupSelectName(group3);
                } else {
                    // 要是分组存在高于上线修改请求状态，将请求状态修改为4
                    await1.setAwaitState(4);
                    bookService.AwaitUpdateState(await1);
                    return new Result(Code.BusinessException.getValue(), "添加失败");
                }
            }
        } else {
            // 当分组存在
            groupList = group1;
        }
        // 当分组存在的时候
//        添加好友
        Buddy buddy = new Buddy();
        buddy.setBuddyGroup(groupList.getGroupId().toString());
        buddy.setBuddyOneselfId(user.getUserMailbox());
        buddy.setBuddyOthersId(leave);
        buddy.setBuddyTime(new Date());
        bookService.BuddyInformation(buddy);

        // 获取次要邮箱添加好友
        Group group2 = new Group();
        group2.setGroupId(Integer.valueOf(user.getUserMailboxType()));
        Group group3 = bookService.GroupSelectId(group2);
//        当分组不存在
        if (group3 == null) {
            return new Result(Code.ERROR.getValue(), "您输入的分组不存在");
        }
        buddy.setBuddyOneselfId(leave);
        buddy.setBuddyOthersId(user.getUserMailbox());
        buddy.setBuddyGroup(user.getUserMailboxType());
        bookService.BuddyInformation(buddy);

        // 删除两条申请信息
        await.setAwaitSecondary(user.getUserMailbox());
        await.setAwaitPrimary(leave);
        Await await2 = bookService.AwaitSelectMailbox(await);
        bookService.AwaitDelete(await1);
        bookService.AwaitDelete(await2);

        return new Result(Code.SUCCESS.getValue(), "添加成功");
    }


    // 根据登录邮箱获取分组信息
    @PostMapping("/GroupNumber")
    public Result GroupNumber(HttpServletRequest request,
                              HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        List<Group> groups = bookService.GroupSelectUserId(leave);

        return new Result(Code.SUCCESS.getValue(), "查询成功", groups);
    }


    // 根据分组名修改分组
    @PostMapping("/GroupUpdateName")
    public Result GroupUpdateName(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestBody Group group) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 判断新分组名是否存在
        Group group1 = new Group();
        group1.setGroupUserId(leave);
        group1.setGroupName(group.getGroupName());
        Group group2 = bookService.GroupSelectName(group1);
        if (group2 != null) {
            return new Result(Code.ERROR.getValue(), "您已经创建了该名称的分组");
        }
        bookService.GroupUpdateName(group);
        return new Result(Code.SUCCESS.getValue(), "修改成功");
    }

    // 新增分组
    @PostMapping("/GroupInsertInformation")
    public Result GroupInsertInformation(HttpServletRequest request,
                                         HttpServletResponse response,
                                         @RequestBody Group group) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        // 根据邮箱和名称查询分组名
        group.setGroupUserId(leave);
        Group group1 = bookService.GroupSelectName(group);
        // 当分组存在报错
        if (group1 != null) {
            return new Result(Code.ERROR.getValue(),
                    "分组名已存在，请勿重复增加");
        }
        // 当分组不存在，判断分组上限
        // 获取全部分组
        List<Group> groups = bookService.GroupSelectUserId(leave);
        // 当存在分组
        if (groups.size() > 0) {
            Integer index = 0;
            Integer Size = 0;
            // 判断是否存在黑名单
            for (int i = 0; i < groups.size(); i++) {
                if (groups.get(i).getGroupName().equals(
                        "黑名单")) {
                    index = 1;
                }
            }
            if (index == 1) {
                Size = 11;
            } else {
                Size = 10;
            }
            if (groups.size() >= Size) {
                return new Result(Code.ERROR.getValue(),
                        "您分组数量已经到达上线");
            }
        }

        // 添加分组
        group.setGroupTime(new Date());
        bookService.GroupInformation(group);
        return new Result(Code.SUCCESS.getValue(), "添加成功", groups);
    }

    // 删除分组
    @PostMapping("/GroupDelete")
    public Result GroupDelete(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestBody Group group) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        // 根据id查询分组

        Group group1 = bookService.GroupSelectId(group);
        // 当分组不存在报错
        if (group1 == null) {
            return new Result(Code.ERROR.getValue(),
                    "分组不存在");
        }
        // 获取全部好友
        Integer index = 0;
        List<Buddy> buddies = bookService.BuddySelectOneself(leave);
        for (int i = 0; i < buddies.size(); i++) {
            if (buddies.get(i).getBuddyGroup().equals(group1.getGroupId().toString())) {
                // 如果分组存在好友
                index = 99;
            }
        }
        // 当分组存在好友
        if (index == 99) {
            return new Result(Code.ERROR.getValue(),
                    "该分组中还有好友，无法删除");
        } else {
            // 当分组没有好友 删除分组
            bookService.GroupDelete(group);
            return new Result(Code.SUCCESS.getValue(), "删除成功");
        }


    }

    // 修改好友分组
    @PostMapping("/GroupUpdateGroup")
    public Result GroupUpdateGroup(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        // 根据id查询分组
        Group group = new Group();
        group.setGroupId(Integer.valueOf(user.getUserMailboxType()));
        Group group1 = bookService.GroupSelectId(group);
        // 当分组不存在报错
        if (group1 == null) {
            return new Result(Code.ERROR.getValue(),
                    "您选择的分组不存在分组不存在");
        }
        // 当分组存在，根据主要邮箱和次要邮箱修改分组id
        Buddy buddy = new Buddy();
        buddy.setBuddyOneselfId(leave);
        buddy.setBuddyGroup(user.getUserMailboxType());
        buddy.setBuddyOthersId(user.getUserMailbox());
        bookService.BuddyUpdateGroup(buddy);

        return new Result(Code.SUCCESS.getValue(), "修改成功");


    }


    // 删除好友
    @PostMapping("/BuddyDelete")
    @Transactional(value = "To")
    public Result BuddyDelete(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        //查询该好友是否存在
        Buddy buddy = new Buddy();
        buddy.setBuddyOneselfId(leave);
        buddy.setBuddyOthersId(user.getUserMailbox());
        Buddy buddy1 = bookService.BuddySelectInformation(buddy);
        if (buddy1 == null) {
            return new Result(Code.ERROR.getValue(), "该用户不是您的好友");
        }

        // 删除好友
        bookService.BuddyDelete(buddy);
        buddy.setBuddyOneselfId(user.getUserMailbox());
        buddy.setBuddyOthersId(leave);
        bookService.BuddyDelete(buddy);

        return new Result(Code.SUCCESS.getValue(), "删除成功");


    }

    // 拉黑好友
    @PostMapping("/BuddyShield")
    @Transactional(value = "To")
    public Result BuddyShield(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        //查询该好友是否存在
        Buddy buddy = new Buddy();
        buddy.setBuddyOneselfId(leave);
        buddy.setBuddyOthersId(user.getUserMailbox());
        Buddy buddy1 = bookService.BuddySelectInformation(buddy);
        if (buddy1 == null) {
            return new Result(Code.ERROR.getValue(), "该用户不是您的好友");
        }
        // 创建一个分组类用于获取最终的黑名单分组
        Group groupList = new Group();
        // 判断是否存在黑名单分组
        Group group = new Group();
        group.setGroupUserId(leave);
        group.setGroupName("黑名单");
        groupList = bookService.GroupSelectName(group);
        if (groupList == null) {
            // 当黑名单分组不存在，创建黑名单分组
            bookService.GroupInformation(group);
            groupList = bookService.GroupSelectName(group);
        }
        // 当存在黑名单，修改好友分组
        Buddy buddy2 = new Buddy();
        buddy2.setBuddyOneselfId(leave);
        buddy2.setBuddyOthersId(user.getUserMailbox());
        buddy2.setBuddyGroup(groupList.getGroupId().toString());
        bookService.BuddyUpdateGroup(buddy2);
        // 删除对方好友信息
        buddy2.setBuddyOneselfId(user.getUserMailbox());
        buddy2.setBuddyOthersId(leave);
        bookService.BuddyDelete(buddy2);
        return new Result(Code.SUCCESS.getValue(), "删除成功");


    }

    // 取消黑名单
    @PostMapping("/CancelBlackList")
    @Transactional(value = "To")
    public Result CancelBlackList(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        //查询该好友是否存在
        Buddy buddy = new Buddy();
        buddy.setBuddyOneselfId(leave);
        buddy.setBuddyOthersId(user.getUserMailbox());
        Buddy buddy1 = bookService.BuddySelectInformation(buddy);
        if (buddy1 == null) {
            return new Result(Code.ERROR.getValue(), "该用户不是您的好友");
        }
        // 获取黑名单分组信息
        Group group = new Group();
        group.setGroupUserId(leave);
        group.setGroupName("黑名单");
        Group group1 = bookService.GroupSelectName(group);
        // 获取所有黑名单分组中的好友
        Buddy buddy2 = new Buddy();
        buddy2.setBuddyOneselfId(leave);
        buddy2.setBuddyGroup(group1.getGroupId().toString());
        List<Buddy> buddies = bookService.BuddySelectMailboxId(buddy2);

        // 排除需要移除黑名单的成员
        List<Buddy> collect =
                buddies.stream().filter(el -> !el.getBuddyOthersId().equals(user.getUserMailbox())).collect(Collectors.toList());
        // 当黑名单分组不包含好友删除黑名单分组,有则不处理
        if (collect.size() <= 0) {
            bookService.GroupDelete(group1);
        }
        // 删除好友信息
        buddy2.setBuddyOthersId(user.getUserMailbox());
        bookService.BuddyDelete(buddy2);

        return new Result(Code.SUCCESS.getValue(), "取消成功");


    }

    // 取消黑名单并重新添加好友申请
    @PostMapping("/CancelBlackListBuddyApply")
    @Transactional(value = "To")
    public Result CancelBlackListBuddyApply(HttpServletRequest request,
                                            HttpServletResponse response,
                                            @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        //查询该好友是否存在
        Buddy buddy = new Buddy();
        buddy.setBuddyOneselfId(leave);
        buddy.setBuddyOthersId(user.getUserMailbox());
        Buddy buddy1 = bookService.BuddySelectInformation(buddy);
        if (buddy1 == null) {
            return new Result(Code.ERROR.getValue(), "该用户不是您的好友");
        }
        // 获取黑名单分组信息
        Group group = new Group();
        group.setGroupUserId(leave);
        group.setGroupName("黑名单");
        Group group1 = bookService.GroupSelectName(group);
        // 获取所有黑名单分组中的好友
        Buddy buddy2 = new Buddy();
        buddy2.setBuddyOneselfId(leave);
        buddy2.setBuddyGroup(group1.getGroupId().toString());
        List<Buddy> buddies = bookService.BuddySelectMailboxId(buddy2);

        // 排除需要移除黑名单的成员
        List<Buddy> collect =
                buddies.stream().filter(el -> !el.getBuddyOthersId().equals(user.getUserMailbox())).collect(Collectors.toList());
        // 当黑名单分组不包含好友删除黑名单分组,有则不处理
        if (collect.size() <= 0) {
            bookService.GroupDelete(group1);
        }
        // 删除好友信息
        buddy2.setBuddyOthersId(user.getUserMailbox());
        bookService.BuddyDelete(buddy2);

        // 添加好友申请
        Await await = new Await();
        await.setAwaitPrimary(leave);
        await.setAwaitSecondary(user.getUserMailbox());
        await.setAwaitGroup(user.getUserMailboxType());
        await.setAwaitType(1);
        await.setAwaitTime(new Date());
        await.setAwaitState(1);
        bookService.AwaitInformation(await);
        await.setAwaitPrimary(user.getUserMailbox());
        await.setAwaitSecondary(leave);
        await.setAwaitGroup(null);
        await.setAwaitType(2);
        bookService.AwaitInformation(await);
        return new Result(Code.SUCCESS.getValue(), "添加成功");


    }

    // 根据邮箱查询用户信息
    @PostMapping("/SelectUserMailbox")
    public Result SelectUserMailbox(HttpServletRequest request,
                                    HttpServletResponse response,
                                    @RequestBody User user) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        //查询该好友是否存在
        Buddy buddy = new Buddy();
        buddy.setBuddyOneselfId(leave);
        buddy.setBuddyOthersId(user.getUserMailbox());
        Buddy buddy1 = bookService.BuddySelectInformation(buddy);
        if (buddy1 == null) {
            return new Result(Code.ERROR.getValue(), "该用户不是您的好友");
        }

        User user1 = bookService.SelectUserMailbox(user.getUserMailbox());
        if (user1 == null) {
            return new Result(Code.ERROR.getValue(), "查询失败");
        }
        // 好友信息存在
        return new Result(Code.SUCCESS.getValue(), "查询成功", user1);


    }


    // 获取聊天信息
    @PostMapping("/ChatSelect")
    public Result ChatSelect(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestBody Chat chat) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        //查询该好友是否存在
//       获取聊天信息
        chat.setChatMailbox(leave);
        List<Chat> chats = bookService.ChatSelectMailbox(chat);
        // 我作为主邮箱屏蔽被删除的内容
        List<Chat> collect = chats.stream().filter(el -> el.getChatMailBoxType() == 1).collect(Collectors.toList());
        chat.setChatMailbox(chat.getChatMailBoxOthers());
        chat.setChatMailBoxOthers(leave);
        List<Chat> chats1 = bookService.ChatSelectMailbox(chat);
        // 我作为次要邮箱屏蔽被删除的内容
        List<Chat> collect1 =
                chats1.stream().filter(el -> el.getChatMailBoxOthersType() == 1).collect(Collectors.toList());
        // 将聊天信息添加到一个新数组中
        List<Chat> chats2 = new ArrayList<>();
        if (collect.size() > 0) {
            for (int i = 0; i < collect.size(); i++) {
                chats2.add(collect.get(i));
            }
        }
        if (collect1.size() > 0) {
            for (int i = 0; i < collect1.size(); i++) {
                chats2.add(collect1.get(i));
            }
        }
        // 进行升序排序
        if (chats2.size() > 0) {
            for (int i = 0; i < chats2.size(); i++) {
                for (int i1 = 0; i1 < chats2.size(); i1++) {
                    if (chats2.get(i).getChatTime().getTime() <= chats2.get(i1).getChatTime().getTime()) {
                        Chat num = chats2.get(i);
                        chats2.set(i, chats2.get(i1));
                        chats2.set(i1, num);
                        num = null;
                    }
                }
            }
        }
// 当类型为图片的时候查询图片url链接
        if (chats2.size() > 0) {
            for (int i = 0; i < chats2.size(); i++) {
                if(chats2.get(i).getChatType().equals("Photo")){
                    Image image = bookService.ImageSelectId(chats2.get(i).getChatText());
                    chats2.get(i).setChatText(image.getImageBlob());
                }
            }
        }


        return new Result(Code.SUCCESS.getValue(), "查询成功", chats2);
    }


    // 上传图片
    @PostMapping("/ImageInformation")
    public Result ImageInformation(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestBody Image image) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        String str = "";
//        生成id
        Integer index=1;

        while (index<2){
            char cha[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
            Integer[] num = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
            Object[] arr = new Object[10];
            // 生成数字个数
            Random random = new Random();
            //设置随机生成的数字数量
            for (int i = 0; i < 5; i++) {
                arr[i] = num[random.nextInt(10)];
            }
            // 设置随机生成的大写字母数量
            for (int i = 5; i < 8; i++) {
                arr[i] = String.valueOf(cha[random.nextInt(26)]);
            }
            // 设置随机生成的小写字母数量
            for (int i = 8; i < 10; i++) {
                arr[i] = String.valueOf(cha[random.nextInt(26)]).toLowerCase();
            }

            for (int i = 0; i < arr.length; i++) {
                str += arr[random.nextInt(arr.length)];
            }
            //  查询生成的id是否存在
            Image image1 = bookService.ImageSelectId(str);
            // 如果id不存在
            if(image1==null){
                index++;
            }
        }

        image.setImageId(str);
        bookService.ImageInformation(image);

        return new Result(Code.SUCCESS.getValue(), "上传成功",str);
    }
    // 删除聊天记录（屏蔽掉聊天记录）
    @PostMapping("/ChatUpdateMailboxType")
    @Transactional(value = "To")
    public Result ChatUpdateMailboxType(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestBody Chat chat) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        Chat chat1 = bookService.ChatSelectId(chat.getChatId());
        if(chat1==null){
            return new Result(Code.ERROR.getValue(), "信息不存在");
        }
        ChatList chatList = new ChatList();

        // 判断我是聊天信息的主要邮箱还是次要邮箱
        if(chat1.getChatMailbox().equals(leave)){
            // 我是主要邮箱
            chat1.setChatMailBoxType(2);
            chatList.setChatListOthersMailbox(chat1.getChatMailBoxOthers());
        }else if(chat1.getChatMailBoxOthers().equals(leave)){
            // 当我作为次要邮箱
            chatList.setChatListOthersMailbox(chat1.getChatMailbox());
            chat1.setChatMailBoxOthersType(2);
        }else{
            // 都不是报错
            return new Result(Code.ERROR.getValue(), "消息删除失败");
        }
        // 更新聊天信息
        if(chat.getChatMailbox().equals("delete")){
            // 更新聊天信息
            chatList.setChatListMailbox(leave);
            ChatList chatList1 = bookService.ChatListSelectMailbox(chatList);
            if(chatList1!=null){
                chatList1.setChatListChatType("delete");
                chatList1.setChatLisOldList("");
                chatList1.setChatListTime(new Date());
                bookService.ChatListText(chatList1);
            }
        }

        bookService.ChatUpdateMailboxType(chat1);
        return new Result(Code.SUCCESS.getValue(), "删除成功");
    }

    // 撤销(删除该条聊天记录)
    @PostMapping("/ChatDelete")
    @Transactional(value = "To")
    public Result ChatDelete(HttpServletRequest request,
                                        HttpServletResponse response,
                                        @RequestBody Chat chat) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 根据id查询聊天信息
        Chat chat1 = bookService.ChatSelectId(chat.getChatId());
        // 判断聊天记录是否存在
        if(chat1==null){
            return new Result(Code.ERROR.getValue(), "您要撤销的信息不存在");
        }
        // 当聊天信息存在，判断聊天时间是否符合撤销时间。十分钟之内都可以撤销
       if( chat1.getChatTime().getTime()+1000*60*10<new Date().getTime()){
           return new Result(Code.ERROR.getValue(), "已超过时间，消息无法撤销");
       }
       // 判断当前是否属于最后一条撤销信息
       if(chat.getChatMailbox().equals("cancel")){
           // 查询聊天主体
           ChatList chatList = new ChatList();
           chatList.setChatListMailbox(leave);
           chatList.setChatListOthersMailbox(chat.getChatMailBoxOthers());
           ChatList chatList1 = bookService.ChatListSelectMailbox(chatList);
           if(chatList1!=null){
               chatList1.setChatListChatType("cancel");
               chatList1.setChatLisOldList("");
               chatList1.setChatListTime(new Date());
               chatList1.setChatListNew(2);
               chatList1.setChatListType(1);
               bookService.ChatListUpdateType(chatList1);
           }
           chatList.setChatListMailbox(chat.getChatMailBoxOthers());
           chatList.setChatListOthersMailbox( leave);
           ChatList chatList2 =
                   bookService.ChatListSelectMailbox(chatList);
           if(chatList2!=null){
               chatList2.setChatListChatType("cancel");
               chatList2.setChatLisOldList("");
               chatList2.setChatListTime(new Date());
               chatList2.setChatListNew(1);
               chatList2.setChatListType(2);
               bookService.ChatListUpdateType(chatList2);
           }
       }


       // 当消息在撤销范围，判断消息是否属于图片类型
        if(chat1.getChatType().equals("Photo")){
            // 当消息类型为图片类型的时候，删除对应的图片消息
         bookService.ImageDelete(chat1.getChatText());
        }
        // 删除消息
        bookService.ChatDelete(chat1.getChatId());
        return new Result(Code.SUCCESS.getValue(), "删除成功");
    }


    // 获取消息列表
    @PostMapping("/ChatListSelect")
    public Result ChatListSelect(HttpServletRequest request,
                             HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 根据id查询聊天信息
        List<ChatList> chatLists = bookService.ChatListSelectMailboxAll(leave);
        if(chatLists.size()>0){
            for (int i = 0; i < chatLists.size(); i++) {
                User user = bookService.UserSelectMailbox(chatLists.get(i).getChatListOthersMailbox());
                chatLists.get(i).setChatListNickname(user.getUserNickname());
            }
        }


        return new Result(Code.SUCCESS.getValue(), "查询成功",
                chatLists);
    }

    // 修改消息列表读取状态
    @PostMapping("/ChatListUpdateNew")
    public Result ChatListUpdateNew(HttpServletRequest request,
                                 HttpServletResponse response,
                                    @RequestBody ChatList chatList) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        // 修改消息读取状态
        chatList.setChatListMailbox(leave);
        // 判断该条记录是否存在，不存在不处理
        ChatList chatList1 = bookService.ChatListSelectMailbox(chatList);
        if(chatList1==null){
            return new Result(Code.ERROR.getValue(), "没有聊天记录");
        }
        chatList.setChatListNew(2);
       bookService.ChatListUpdateNew(chatList);
        return new Result(Code.SUCCESS.getValue(), "查询成功");
    }


    // 查询个人信息
    @PostMapping("/UserSelectMailbox")
    public Result UserSelectMailbox(HttpServletRequest request,
                                    HttpServletResponse response) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }

        User user = bookService.UserSelectMailbox(leave);
        if(user==null){
            return new Result(Code.ERROR.getValue(), "用户不存在");
        }
        return new Result(Code.SUCCESS.getValue(), "查询成功",user);
    }



    // 修改主题色

    // 查询个人信息
    @PostMapping("/SettingUpdateTheme")
    public Result SettingUpdateTheme(HttpServletRequest request,
                                    HttpServletResponse response,
                                     @RequestBody Settings settings) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        settings.setSettingMailbox(leave);
       bookService.SettingUpdateTheme(settings);
        return new Result(Code.SUCCESS.getValue(), "修改完成");
    }


    // 查询个人信息
    @PostMapping("/SettingUpdateSidebar")
    public Result SettingUpdateSidebar(HttpServletRequest request,
                                     HttpServletResponse response,
                                     @RequestBody Settings settings) {
        String leave = Leave(request);
        if (leave.equals("401")) {
            response.setStatus(401);
            return new Result(Code.ERROR.getValue(), "您没有权限");
        }
        settings.setSettingMailbox(leave);
        bookService.SettingUpdateSidebar(settings);
        return new Result(Code.SUCCESS.getValue(), "修改完成");
    }

    // 权限判断
    private String Leave(HttpServletRequest request) {
        Request request1 = new Request();
        String axios = request1.Axios(request);
        Token token = new Token();
        String token1 = token.getToken(axios);
        if (token1 == null) {
            return "401";
        } else if (token1.equals("401")) {
            return "401";
        }
        User user = bookService.UserSelectMailbox(token1);
        if (user == null) {
            return "401";
        }
        return token1;
    }


}






